package packets;

import java.util.ArrayList;
import java.util.List;

import org.jnetpcap.nio.JBuffer;
import org.jnetpcap.packet.JHeader;
import org.jnetpcap.packet.JPacket;
import org.jnetpcap.packet.JRegistry;
import org.jnetpcap.packet.RegistryHeaderErrors;
import org.jnetpcap.packet.annotate.Bind;
import org.jnetpcap.packet.annotate.Field;
import org.jnetpcap.packet.annotate.Header;
import org.jnetpcap.packet.annotate.HeaderLength;

/**
 * Diese Klasse repräsentiert ein OSPF Database Description Paket und hat Methoden, die die entsprechenden Felder zurück geben.
 * @author pascal
 *
 */
@Header
public class OSPFDescription extends JHeader {

	private ArrayList<LSA> lsaList;
	
	public ArrayList<LSA> getLSAs(){
		return this.lsaList;
	}
	
	@Field(offset = 0*8, length=2*8)
	public int interfaceMTU(){
		return getUShort(0);
	}
	
	@Field(offset = 2*8, length=1*8)
	public int options(){
		return getUByte(3);
	}
	
	@Field(offset = 3*8, length = 1*8)
	public byte IMMS(){
		return getByte(3);
	}
	
	@Field(offset = 4*8, length = 4*8)
	public long DDSequenceNumber(){
		return getUInt(4);
	}
	
	@HeaderLength
	public static int headerLength(JBuffer buffer, int offset){
		
		return buffer.size() - offset;
		
	}
	
	@Bind(to = OSPF.class)
	public static boolean bindToOSPF(JPacket packet, OSPF ospf){
		return ospf.type() == 0x02;
	}
	
	
	/**
	 * Findet alle LSAs die in diesem Paket enthalten sind.
	 */
	@Override
	protected void decodeHeader() {
		int offsetIntoHeader = 8;
		int lsaCount = (this.size() - 8) / 20;

		ArrayList<LSA> lsas = new ArrayList<LSA>();
		
		for(int i = 0; i < lsaCount; i++){
			LSA tempLSA = new LSA();
			tempLSA.peer(this, offsetIntoHeader+ (i*20), 20);
			
			lsas.add(tempLSA);
		}
		
		this.lsaList = lsas;
	}
	
	static {
		try {
			JRegistry.register(OSPFDescription.class);
		}catch(RegistryHeaderErrors e){
			e.printStackTrace();
		}
	}
	
	

}
